Ext.define('App.controller.Main', {
	extend: 'Ext.app.Controller',
	views: ['Viewport', 'Header', 'Footer', 'Center', 'Left', 'PasswordEdit', 'Frame'],
	stores: ['Menu'],
	refs: [{
		ref: 'center',
		selector: 'appcenter'
	}],

	init: function () {
		this.control({
			'appleft': {
				select: this.onMenuSelect
			},
			'passwordedit button[action=save]': {
				click: this.onSave
			},
			'appheader button[action=logout]': {
				click: this.onLogout
			},
			'appheader button[action=password]': {
				click: this.onChangePassword
			}
		});
	},
	onChangePassword: function () {
		var edit = Ext.widget("passwordedit");
		edit.show();
	},
	//退出登录
	onLogout: function () {
		window.location.href = 'http://localhost:8080/sso/sso/so';
	},
	onSave: function (me, e, eo) {
		var win = me.up('window');
		var form = win.down('form');
		var old = win.down("textfield[name=password]").value;
		var now = win.down("textfield[name=confirm]").value;
		if (old != now) {
			Ext.example.msg('错误', '密码不一致。');
			return;
		}
		if (form.isValid()) {
			form.submit({
				clientValidation: true,
				url: 'user/password',
				method: 'post',
				success: function (form, action) {
					Ext.example.msg('提示', '修改密码成功。');
					win.close();
				},
				failure: function (form, action) {
					var result = Ext.JSON
						.decode(action.response.responseText);
					Ext.Msg.alert('提示', result.message);
				}
			});
		}
	},
	//菜单被选择
	onMenuSelect: function (me, rec, i, eo) {
		if (rec.isLeaf()) {
			var centerTab = this.getCenter();
			var id = "panel" + rec.getId();
			// if (id == 'passwordedit') {
			// 	var edit = Ext.widget("passwordedit");
			// 	edit.show();
			// 	return;
			// }
			var panel = centerTab.getComponent(id);
			if (!panel) {
				var title = rec.get('text');
				panel = new Ext.Panel({
					id: id,
					xtype: id,
					title: title,
					closable: true,
					closeAction: 'destroy',
					iconCls: id,
					html: '<iframe src="' + rec.raw.myurl + '" frameborder="0" width="100%" height="100%"></iframe>'
				});

				centerTab.add(panel);
			}
			centerTab.setActiveTab(panel);
		}
	}
});